package com.chinasoft.controller;

import com.chinasoft.pojo.*;
import com.chinasoft.service.Impl.UserlistServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/Userlist")
public class UserlistController {
    @Autowired
    UserlistServiceImpl userlistService;

    @PostMapping("/addlist")
    // ResponseBody 将json字符串 转换为java 列表
    public Result insertRec(@RequestBody Userlist brand) {

        System.out.println(brand);
        boolean insertBrand = userlistService.addlist(brand);
        if (insertBrand) {
            return new Result(brand, "添加成功");
        } else {
            return Result.fail(500, "添加失败");
        }
    }

    @PostMapping("/search/{username}")
    public List<Userlist> list(@PathVariable String username){
        return userlistService.getByName(username);
    }

    @PostMapping("/findByname")
    public Result getByclass(@RequestBody Userlist userlist){
        boolean found = userlistService.getClass(userlist.getClassid(), userlist.getUsername());
        if(found) {
            return new Result(200, "查询成功");
        }else{
            return new Result(500, "查询失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean delete = userlistService.deleteById(id);
        if (delete) {
            return new Result(null, "删除成功");
        } else {
            return Result.fail(500, "删除失败");
        }
    }

    @PostMapping("/searchBy")
    public List<Userlist> listBy(@RequestBody Userlist brand) {
        return userlistService.find(brand);
    }

    @PostMapping("/searchByDel")
    public List<Userlist> search(@RequestBody Userlist brand){
        return userlistService.search(brand.getUsername(), brand.getDel());
    }

    @GetMapping("/findById/{classid}")
    public List<Userlist> findById(@PathVariable Integer classid){
        return userlistService.findById(classid);
    }

    @PostMapping("/dailnum")
    @ResponseBody
    public Result<GoodCount> getDailSalesByDays() {
        List<GoodCount> result = userlistService.getMonth();
        return new Result(result, "查询成功");
    }

    @PostMapping("/list/searchTime")
    public Result updateTime(@RequestBody Userlist OrderUpdate) {
        boolean updateCode = userlistService.getTime(OrderUpdate.getUsername(), OrderUpdate.getClasstype(), OrderUpdate.getTime());
        if(updateCode) {
            return new Result(200, "查找成功");
        }else{
            return new Result(500, "查找成功");
        }
    }
}
